package cn.sumpu.app.wlcity.web.mvc.vote;


import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.whty.aam.client.authentication.AttributePrincipal;
import com.whty.aam.client.util.AAMUtils;
import com.whty.aam.client.util.EncryptUtils;

import cn.sumpu.app.wlcity.biz.ContentBiz;
import cn.sumpu.app.wlcity.biz.VoteBiz;
import cn.sumpu.app.wlcity.domain.Content;
import cn.sumpu.app.wlcity.domain.PageModel;
import cn.sumpu.app.wlcity.domain.Vote;
import cn.sumpu.app.wlcity.domain.VoteItem;
import cn.sumpu.app.wlcity.exception.AppBusinessException;
import cn.sumpu.app.wlcity.util.Common;

@Controller
public class VoteFrontController {
	private static int maxVoteCount=10;//一个号码每日最大投票次数
	@Autowired
	private VoteBiz voteBiz;
	@Autowired
	private ContentBiz contentBiz;
	//投票首页
	@RequestMapping(value = "/poll.htm")
	public String toVote() {
		return "poll/index";
	}
	
	//内容列表页
	@RequestMapping(value = "/content.htm")
	public String hot(HttpServletRequest request,Integer page,String contentType) {
		try {
			page=page==null?1:page;
			String app =Common.APP_VOTE;
			List<Content> contentList = contentBiz.getContentHotList(page,contentType,app);
			int size = contentBiz.getContentListCount(contentType,app);
			int totalPage = (size + Common.PAGE_SIZE_HOT - 1)/ Common.PAGE_SIZE_HOT;
			request.setAttribute("totalPage", totalPage);
			request.setAttribute("contentList", contentList);
			//request.setAttribute("link", "hot.htm");
			request.setAttribute("currentPage", page);
			request.setAttribute("contentType", contentType);
		} catch (AppBusinessException e) {
			e.printStackTrace();
			return "hotspring/error";
		}
		return "poll/content/contentList";
	}
	
	//内容详细页
	@RequestMapping(value = "/contentDetail.htm")
	public String hotDetail(String id,HttpServletRequest request) {
		try {
			Content content = contentBiz.getContentById(id);
			request.setAttribute("content", content);
		} catch (AppBusinessException e) {
			e.printStackTrace();
			return "hotspring/error";
		}
		return "poll/content/contentDetail";
	}
	//投票首页
	@RequestMapping(value = "/rank.htm")
	public String rank() {
		return "poll/vote/rank";
	}
	
	//投票首页
	@RequestMapping(value = "/rankItem.htm")
	public String rankItem(HttpServletRequest request,String vtype,Integer page) {
		try {
			PageModel pm=voteBiz.getVoteRank(page, vtype);
			request.setAttribute("pageModel", pm);
			request.setAttribute("link", "rankItem.htm?vtype="+vtype);
			request.setAttribute("voteType", vtype);
		} catch (AppBusinessException e) {
			e.printStackTrace();
		}
		return "poll/vote/rankItem";
	}
	//投票首页
	@RequestMapping(value = "/pVote.htm")
	public String vote(HttpServletRequest request, Integer page,String phone) {
		try {
			// 以下代码取用户的手机号码
			AttributePrincipal principal = (AttributePrincipal)request.getUserPrincipal();
			if (null != principal) {
				String username = principal.getName();
				JSONObject obj =AAMUtils. getUserInfo("http://218.201.202.232:8098/aam",username);
				String mobilephone = obj.getString("phone");
				if (mobilephone != null) {
					//mobilephone = EncryptUtils.decode(mobilephone);
					request.getSession(true).setAttribute("mobilephone", mobilephone);
				}
			}
			// 以上代码取用户的手机号码
			
			page=page==null?1:page; 
			String app=Common.APP_VOTE;
			PageModel pageModel = voteBiz.getFrontVotePageModel(page,app); 
			request.setAttribute("pageModel", pageModel);
			request.setAttribute("app", app);
			request.setAttribute("phone", phone);
			request.setAttribute("link", "pVote.htm");
		} catch (Exception e) {
			e.printStackTrace();
			return "hotspring/error";
		}
		return "poll/vote/vote";
	}

	// 对列表项投票
	@RequestMapping(value = "/votePhone.htm")
	public String pollVoteIt(HttpServletRequest request,
			HttpServletResponse response, String voteItemId, String phone,String app) {
		try {
			response.setContentType("text/html; charset=UTF-8"); 
			response.setHeader("Cache-Control", "no-cache");  
			PrintWriter pw = response.getWriter();
			SimpleDateFormat format= new SimpleDateFormat("yyyy-MM-dd");
			String date=format.format(new Date());
			int voteCount=voteBiz.getVoteCountByPhoneDate(phone, date);
			Boolean voteResult=false;
			if(voteCount<maxVoteCount){
				voteResult=voteBiz.insertVoteUsePhone(voteItemId, phone,app);
				if(voteResult){
					pw.write("success");
				}
			}else{
				pw.write("lot");
			}
				
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	//投票项列表页
	@RequestMapping(value = "/pItem.htm")
	public String getVoteItemList(HttpServletRequest request, String voteId,Integer page) {
		try {
			PageModel pageModel = voteBiz.getVoteItemLists(voteId,page);
			Vote vote=voteBiz.getVoteById(voteId);
			request.setAttribute("pageModel", pageModel);
			request.setAttribute("voteId", voteId);
			request.setAttribute("voteTitle", vote.getTitle());
			request.setAttribute("link", "pItem.htm?voteId="+voteId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "poll/vote/voteItemList";
	}
	
	//投票项详细页
	@RequestMapping(value = "/pDetail.htm")
	public String getVoteItemById(HttpServletRequest request, String voteItemId,String voteItemTitle) {
		try {
			VoteItem voteItem = voteBiz.getVoteItemById(voteItemId);
			String voteId=voteItem.getVoteId();
			Vote vote=voteBiz.getVoteById(voteId);
			request.setAttribute("voteId", voteItem.getVoteId());
			request.setAttribute("voteTitle", vote.getTitle());
			request.setAttribute("voteItem", voteItem);
			request.setAttribute("vote", vote);
		} catch (AppBusinessException e) {
			e.printStackTrace();
		}
		return "poll/vote/voteItemDetail";
	}
}
